package com.droidbank.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;


import com.droidbank.model.Account;
import com.droidbank.model.DataModel;
import com.droidbank.model.Expense;


public class DateComparison {

	private static Calendar cal = Calendar.getInstance();
	
	public static DataModel dm;
	
	
	public static Vector<Expense> getAllExpenseBeforeDate(String accountName, int month, int day, int year)
	{
		Vector<Expense> results = new Vector<Expense>();
		
		cal.set(year, month, day);
		Date d = cal.getTime();
		
		for(Account a:dm.getAccounts())
		{
			if(a.getName().equals(accountName))
			{
				for(Expense e: a.getExpenses())
				{
					if(new Date(e.getDate()).before(d))
					{
						results.add(e);
					}
				}
			}
		}
		
		
		return results;
		
	}
	
	public static Vector<Expense> getAllExpenseAfterDate(String accountName, int month, int day, int year)
	{
		Vector<Expense> results = new Vector<Expense>();
		
		cal.set(year, month, day);
		Date d = cal.getTime();
		
		for(Account a:dm.getAccounts())
		{
			if(a.getName().equals(accountName))
			{
				for(Expense e: a.getExpenses())
				{
					if(new Date(e.getDate()).after(d))
					{
						results.add(e);
					}
				}
			}
		}
		
		
		return results;
		
	}
	
	public static Vector<Expense> getAllExpenseInBetweenDates(
			String accountName, int monthb, int dayb, int yearb,
			int monthe, int daye, int yeare)
	{
		Vector<Expense> results = new Vector<Expense>();
		
		cal.set(yearb, monthb, dayb);
		Date beginning = cal.getTime();
		
		cal.set(yeare, monthe, daye);
		Date end = cal.getTime();
		
		for(Account a:dm.getAccounts())
		{
			if(a.getName().equals(accountName))
			{
				for(Expense e: a.getExpenses())
				{
						if(new Date(e.getDate()).after(beginning) 
								&& new Date(e.getDate()).before(end))
						{
							results.add(e);
						}
					
				}
			}
		}
		
		
		return results;
		
	}
	
	public static Vector<Expense> getAllExpenseFromMonth(
			String accountName, int month)
	{
		Vector<Expense> results = new Vector<Expense>();
		if(month <1 || month >12){return results;}
		
		for(Account a:dm.getAccounts())
		{
			if(a.getName().equals(accountName))
			{
				for(Expense e: a.getExpenses())
				{
						if(new Date(e.getDate()).getMonth() == month)
						{
							results.add(e);
						}
				}
			}
		}
		
		
		return results;
		
	}

	
	public static Hashtable<String, Double> getAllTypeAmount(int yearb, int yeare)
	{
		cal.set(yearb, 0, 1);
		Date begin = cal.getTime();
		
		cal.set(yeare, 0, 1);
		Date end = cal.getTime();
		
		
		Hashtable<String, Double> total = new Hashtable<String, Double>();
		
		
		for(Account a:dm.getAccounts())
		{
				for(Expense e: a.getExpenses())
				{
					if(e.getType().equals(Expense.TRANSFER)){continue;}
				
					Date eDate = new Date(e.getDate());
					
					//if years match
					if((eDate.after(begin) 
							&& eDate.before(end) )||
							eDate.getYear() == begin.getYear()  
							|| eDate.getYear() == end.getYear())
					{
					
					if(total.containsKey(e.getType()))
					{
						total.put(e.getType() , total.get(e.getType())+ e.getAmount());
						
					}
					else
						total.put(e.getType(), e.getAmount());
					}	
				}
		}
		
		return total;
	}
	
	public static Hashtable<String, TotalExpense> getAllTypeAmount(int yearb, int monthb, int dayb, 
			int yeare, int monthe, int daye)
	{
		cal.set(yearb, monthb, dayb);
		Date begin = cal.getTime();
		
		cal.set(yeare, monthe, daye);
		Date end = cal.getTime();
		
		
		Hashtable<String, TotalExpense> total = new Hashtable<String, TotalExpense>();
		
		
		for(Account a:dm.getAccounts())
		{
				for(Expense e: a.getExpenses())
				{
					if(e.getType().equals(Expense.TRANSFER)){continue;}
				
					Date eDate = new Date(e.getDate());
					
					//if years match
					if((eDate.after(begin) && eDate.before(end) )
							||
							(eDate.getYear() == begin.getYear()  || eDate.getYear() == end.getYear())
							&&
							(eDate.getMonth()== begin.getMonth() || eDate.getMonth() == end.getMonth())
							&&
							(eDate.getDate() == begin.getDate() || eDate.getDate() == end.getDate()))
					{
					
					if(total.containsKey(e.getType()))
					{
						TotalExpense te = total.get(e.getType());
						te.total+= e.getAmount();
						total.put(e.getType(),te);
						
					}
					else
					{
						TotalExpense te = new TotalExpense();
						te.total = e.getAmount();
						total.put(e.getType(), te);
					}
						
					}	
				}
		}
		
		//get total amount
		double totalAmountPos =0;
		double totalAmountNeg =0;
		for(TotalExpense te: total.values())
		{
			if(te.total > 0)
				totalAmountPos+=te.total;
			else
				totalAmountNeg+=te.total;
		}
		
		//cal percent
		for(TotalExpense te: total.values())
		{
			if(te.total < 0)
				te.percent = ((te.total)/totalAmountNeg) *100;
			else
				te.percent = -((te.total)/totalAmountPos) *100;
		}
		
		
		return total;
	}
	
	public static Vector<Date> getMaxBeginAndEndDates()
	{
		Vector<Date> be = new Vector<Date>(2);
		Date beg = null;
		Date end = null;
		
		
		for(Account a:dm.getAccounts())
		{
			for(Expense e: a.getExpenses())
			{
				if(beg == null){beg = new Date(e.getDate());}
				if(end == null){end = new Date(e.getDate());continue;}
				
				Date eDate = new Date(e.getDate());
				if(eDate.before(beg)){beg = eDate;}
				if(eDate.after(end)){end = eDate;}
				
			}
		}
		
		be.add(beg);
		be.add(end);
		return be;
	}
	
	public static Date getDate(int month, int day , int year)
	{
		cal.set(year, month, day);
		return cal.getTime();
	}
	
}
